<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form
          ref="form"
          :rules="rules"
          :model="form"
          label-width="100px"
          style="padding-right: 20px"
        >
          <el-form-item label="签到主题" prop="title">
            <el-input v-model.trim="form.title"></el-input>
          </el-form-item>
          <el-form-item label="应到人数">
            <el-input-number
              v-model="form.dueNum"
              :min="1"
              :max="1000"
              size="medium"
              label="应到人数"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="签到时间">
            <el-date-picker
              v-model="form.signTime"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd HH:mm"
              value-format="yyyy-MM-dd HH:mm"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model.trim="form.addr"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <!-- <el-button
          type="custom"
          v-if="!isAdd"
          v-preventReClick
          @click="handleDelete"
          >删 除</el-button
        > -->
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: {
    ChapterSelect: () => import('@/components/dataSelect/chapterSelect'),
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      form: {
        title: '', //签到主题
        resId: '', //资源id
        resType: '1', //资源类型1-章节，2-其他扩展
        actNum: '100', //实到人数
        dueNum: '1', //应到人数
        startTime: '', //开始签到时间 2023-04-13 9:00
        endTime: '', //截止截止时间 2023-04-13 9:30
        addr: '', //地址
        signTime: '',
      },
      rules: {
        title: [{ required: true, message: '请输入签到主题', trigger: 'blur' }],
      },
      current: {},
      isAdd: false,
    }
  },
  props: ['lessonId'],
  mounted() {
    this.$bus.$on('showDialogSignInAdd', this.init)
  },
  beforeDestroy() {
    this.$bus.$off('showDialogSignInAdd')
  },
  methods: {
    //显示弹窗
    async init(row) {
      let obj = {
        page: 1,
        limit: 99,
        resId: row.id,
        resType: '1',
      }
      let res = await this.$store.dispatch('queryeduSignIn', obj)
      let data = res.data
      this.isAdd = data.length == 0
      this.title = this.isAdd ? '新增' : '编辑'
      if (this.isAdd) {
        this.form = this.$options.data().form
        this.form.title = row.name
        this.form.resId = row.id
        this.$delete(this.form, 'id')
      } else {
        this.current = data[0]
        let { id, startTime, endTime } = this.current
        this.form = { ...this.form, ...this.current }
        this.$set(this.form, 'id', id)
        if (startTime && endTime) {
          this.form.signTime = [startTime, endTime]
        }
      }
      this.dialogVisible = true
    },
    submitForm(form) {
      this.form.startTime = this.form.signTime[0]
      this.form.endTime = this.form.signTime[1]

      this.$refs[form].validate(async (valid) => {
        if (valid) {
          let dispatchType =
            this.title == '新增' ? 'addeduSignIn' : 'updateeduSignIn'
          await this.$store.dispatch(dispatchType, this.form)
          this.successMsg('操作成功')
          // this.$bus.$emit('refreshLessonPlanList')
          this.dialogVisible = false
        }
      })
    },
    resetForm(form) {
      this.$refs[form].resetFields()
    },
    setChapterId(v) {
      this.form.chapterId = v
    },
    handleDelete() {
      this.$store.dispatch('deleteeduSignIn', this.current)
      this.dialogVisible = false
    },
  },
}
</script>